﻿using System;
using DevExpress.XtraBars.Docking2010.Views.WindowsUI;
using EduFormManager.Models;
using EduFormManager.Models.Repo;
using EduFormManager.Properties;

namespace EduFormManager.Forms.UserControls
{
    public partial class XtraMessageControl : XtraBaseControl, ISupportDocumentActions
    {
        public enum MessageTypeEnum
        {
            RequestForQuery,
            BugReport
        }

        private MessageTypeEnum m_messageType;

        public XtraMessageControl(WindowsUIView view) 
            : base(view)
        {
            InitializeComponent();
            this.allformBindingSource.DataSourceChanged += allformBindingSource_DataSourceChanged;
        }

        void allformBindingSource_DataSourceChanged(object sender, EventArgs e)
        {
            this.comboBoxForm.Properties.Items.Clear();
            this.comboBoxForm.Properties.Items.AddRange(this.allformBindingSource.List);
        }

        public MessageTypeEnum MessageType
        {
            set
            {
                m_messageType = value;
                TrigControlsByMessageType(value);
            }
            get
            {
                return m_messageType;
            }
        }

        private void TrigControlsByMessageType(MessageTypeEnum type)
        {
            switch (type)
            {
                case MessageTypeEnum.RequestForQuery:
                    this.label1.Visible = true;
                    this.comboBoxForm.Visible = true;
                    this.label2.Text = "Сообщение";
                    this.textBoxMessage.Text = "";
                    break;
                case MessageTypeEnum.BugReport:
                    this.label1.Visible = false;
                    this.comboBoxForm.Visible = false;
                    this.textBoxMessage.Visible = true;
                    this.comboBoxForm.Properties.Items.Clear();
                    this.comboBoxForm.SelectedItem = null;
                    this.label2.Text = "Опишите последовательность действий, при которой возникает обнаруженная вами ошибка";
                    this.textBoxMessage.Text = "";
                    break;
            }
        }

        public bool CanSend()
        {
            return CanSend(null);
        }

        public bool CanSend(Document doc)
        {
            switch (m_messageType)
            {
                case MessageTypeEnum.RequestForQuery:
                    return comboBoxForm.SelectedItem != null && !String.IsNullOrEmpty(textBoxMessage.Text);
                case MessageTypeEnum.BugReport:
                    return !String.IsNullOrEmpty(textBoxMessage.Text);
                default:
                    return false;
            }
        }

        public void Send()
        {
            Send(null);
        }

        public void Send(Document doc)
        {
            if (!this.CanSend())
            {
                base.ShowFlyoutMessageBox("Ошибка", "Сообщение не должно содержать пустых полей.", FlyoutCommand.OK);
                return;
            }
            Allform form = this.comboBoxForm.SelectedItem as Allform;
            Message message = new Message() 
            { 
                Body = this.textBoxMessage.Text,
                FromEdu = Authentication.CurrentCredentials.EduOrg, 
                SendDate = DateTime.Now 
            };
            switch (m_messageType)
            {
                case MessageTypeEnum.RequestForQuery:
                    message.Subject = String.Format("Запрос для формы {0} от организации {1}", form, Authentication.CurrentCredentials.EduOrg);
                    break;
                case MessageTypeEnum.BugReport:
                    message.Subject = String.Format("Ошибка", form);
                    break;
                default:
                    return;
            }
            RepoHelper.Repo.Save(message);
            this.ShowFlyoutMessageBox("Информация", "Сообщение успешно отправлено.", FlyoutCommand.OK);
            //EmailMessaging.SendMessage(message.Subject, message.Body);
        }

        public void OnQueryDocumentActions(IDocumentActionsArgs args)
        {
            args.DocumentActions.Add(new DocumentAction(Send) { Caption = "Отправить", Image = Resources.mail });
        }
    }
}
